unit Chromium4D.Extension.Natives;

interface

  uses System.Classes, System.SysUtils, ceflib
    , Chromium4D.Core
    , Chromium4D.Render.Bundle
    , Chromium4D.Render.Environment
    , Chromium4D.Extension
    ;

type

  TSyExtNatives = class(TSyV8ExtHandler)
  private
    procedure DefineJavascript(env: TSyRenderEnvironment; ctx: ICefv8Context; target: ICefv8Value);
  public
    class function ObjectFactory(process: TSyChromiumProcess; env: TSyRenderEnvironment; context: ICefv8Context): ICefv8Value; override;
  end;

implementation

{ TSyExtNatives }

procedure TSyExtNatives.DefineJavascript(env: TSyRenderEnvironment; ctx: ICefv8Context; target: ICefv8Value);
var
  I: Integer;
  source: TStringList;
  sourceFile: string;
begin
  source := TStringList.Create;
  try
    for I := 0 to High(EXTENSION_MODULES) do
    begin
      sourceFile := ExtractFilePath(ParamStr(0)) + '\native\' + EXTENSION_MODULES[I][0] + '.js';
      if FileExists(sourceFile) then
      begin
        source.LoadFromFile(sourceFile);
        target.SetValueByKey(EXTENSION_MODULES[I][0], TCefv8ValueRef.NewString(source.Text), V8_PROPERTY_ATTRIBUTE_NONE);
      end;
    end;
  finally
    source.Free;
  end;
end;

class function TSyExtNatives.ObjectFactory(process: TSyChromiumProcess; env: TSyRenderEnvironment; context: ICefv8Context): ICefv8Value;
var
  ObjectFactory: TSyExtNatives;
begin
  ObjectFactory := TSyExtNatives.Create;
  Result := TCefv8ValueRef.NewObject(nil);
  ObjectFactory.DefineJavascript(env, context, Result);
end;

end.
